
#import "GCManager.h"
#import "GKAchievementHandler.h"
#import "OpenFeint/OFHighScoreService.h"
#import "OpenFeint/OFAchievementService.h"

NSString* sLeaderboardID[2] = {
	@"Devourer.Classic",
	@"Devourer.Survival"
};

NSString* sAchievementID[21] = {
	@"Devourer.Novice",			@"Devourer.Apprentice",		@"Devourer.Expert",
	@"Devourer.Elite",			@"Devourer.Master",			@"Devourer.Sergeant",
	@"Devourer.Captain",		@"Devourer.Colonel",		@"Devourer.General",
	@"Devourer.Preventer",		@"Devourer.Survivor",		@"Devourer.Impossible",
	@"Devourer.Perfectionist",	@"Devourer.Shopper",		@"Devourer.Hoarder",
	@"Devourer.Social",			@"Devourer.Fan",			@"Devourer.TrueFan",
	@"Devourer.Addict",			@"Devourer.Sleep",			@"Devourer.Tragic",
};

NSString* sOFLeaderboardID[2] = {
	@"886487",	@"886497"
};

NSString* sOFAchievementID[21] = {
	@"1197862",	@"1197872",	@"1197882",	@"1197892",	@"1197902",	@"1197912",	@"1197922",
	@"1197932",	@"1197942",	@"1197952",	@"1197962",	@"1197972",	@"1197982",	@"1197992",
	@"1198002",	@"1198012",	@"1198022",	@"1198032",	@"1198042",	@"1198052",	@"1198062",
};

@implementation GCManager

@synthesize isLogin = mLogin, finishedAmCount = mFinishedAmCount;

-(id)initWithView:(UIViewController*)view {
	if((self = [super init])) {
		mLogin = false;
		mView = view;
		mFinishedAmCount = -1;
		mAmDict = [[NSMutableDictionary alloc] init];
		mAmDescDict = [[NSMutableDictionary alloc] init];
		[self checkSupport];
	}
	return self;
}

-(void)dealloc
{
	[mAmDict release];
	[mAmDescDict release];
	[super dealloc];
}

// 对Game Center支持判断
-(void)checkSupport
{
	Class gcClass = (NSClassFromString(@"GKLocalPlayer"));
	NSString *reqSysVer = @"4.1";
	NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
	bool osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);

	mSupport = gcClass && osVersionSupported;
}

// 用户登录
-(void)authenticateLocalPlayer
{
	if(mSupport)
	{
		GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];
		[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
			if (error == nil) {
//				[self resetAchievements];
				//成功处理
				[self loadAchievements];
			}
		}];
	}
}

// 读取所有成就
-(void)loadAchievements
{
	[GKAchievement loadAchievementsWithCompletionHandler:^(NSArray* ams,NSError* error) {
		if (error == nil) {
			mFinishedAmCount = 0;
			for(GKAchievement* am in ams) {
				if(am.completed)
				{
					[mAmDict setObject:am forKey:am.identifier];
					mFinishedAmCount++;
				}
			}
			[GKAchievementDescription loadAchievementDescriptionsWithCompletionHandler:^(NSArray* descs, NSError* err) {
				if(err == NULL) {
					mLogin = true;
					if(descs != NULL)
						for(GKAchievementDescription* d in descs)
							[mAmDescDict setObject:d forKey:d.identifier];
				}
			}];
		}
	}];
}

// 显示排行榜
-(void)showLeaderboard:(int)no
{
	if(mLogin)
	{
		GKLeaderboardViewController* lbCtrl = [[GKLeaderboardViewController alloc] init];
		lbCtrl.category = sLeaderboardID[no-1];
		lbCtrl.leaderboardDelegate = self;
		[mView presentModalViewController:lbCtrl animated:YES];
		[lbCtrl release];
	}
}

-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController*)viewController
{
	[mView dismissModalViewControllerAnimated:YES];
}

// 显示成就
-(void)showAchievements
{
	if(mLogin)
	{
		GKAchievementViewController* amCtrl = [[GKAchievementViewController alloc] init];
		amCtrl.achievementDelegate = self;
		[mView presentModalViewController:amCtrl animated:YES];
		[amCtrl release];
	}
}

- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
	[mView dismissModalViewControllerAnimated:YES];
}

// 汇报一个成就的进度
-(void)reportAm:(int)no percent:(float)percent
{
	// OpenFeint
	OFDelegate nilDelegate;
	[OFAchievementService updateAchievement:sOFAchievementID[no-1] andPercentComplete:percent andShowNotification:NO ];

	if(mLogin) {
		if(![mAmDict objectForKey:sAchievementID[no-1]]) {
			GKAchievement* am = [[[GKAchievement alloc] initWithIdentifier:sAchievementID[no-1]] autorelease];
			am.percentComplete = percent;
			[am reportAchievementWithCompletionHandler:^(NSError* error) {
				if (error == NULL) {
					if(percent == 100) {
//						NSLog(@"Report new achievement: %@", sAchievementID[no-1]);
						[mAmDict setObject:am forKey:am.identifier];
						mFinishedAmCount++;
						[[GKAchievementHandler defaultHandler] notifyAchievement:[mAmDescDict objectForKey:am.identifier]];
					}
				}
			}];
		}
	}
}

// 上传一个分数
-(void)reportScore:(int)score category:(int)no
{
	// OpenFeint
	OFDelegate nilDelegate;
	[OFHighScoreService setHighScore:score forLeaderboard:sOFLeaderboardID[no-1] onSuccess:nilDelegate onFailure:nilDelegate];

	if(mLogin) {
		GKScore* sr = [[[GKScore alloc] initWithCategory:sLeaderboardID[no-1]] autorelease];
		sr.value = score;
		[sr reportScoreWithCompletionHandler:^(NSError *error) {
			if (error == nil) {
			}
		}];
	}
}

-(void)resetAchievements
{
	[GKAchievement resetAchievementsWithCompletionHandler:^(NSError *error) {
		if (error == NULL) {
			[mAmDict release];
			mAmDict = [[NSMutableDictionary alloc] init];
			mFinishedAmCount = 0;
		}
	}];
}

@end
